//
//  RegAgreementViewController.m
//  myhuibao
//
//  Created by lijun on 13-4-23.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//

#import "RegAgreementViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h"
#import "OMGToast.h"

@interface RegAgreementViewController ()

@end

@implementation RegAgreementViewController
@synthesize webView;
@synthesize svMain;
@synthesize req;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    svMain.pagingEnabled = YES;
//    svMain.clipsToBounds = NO;
    //    svMain.contentSize = CGSizeMake(svMain.frame.size.width, svMain.frame.size.height*2);
    svMain.showsHorizontalScrollIndicator = NO;
//    svMain.showsVerticalScrollIndicator = NO;
    //    svMain.scrollsToTop = NO;
    //svMain.delegate = self;
    //    [svMain setContentSize:CGSizeMake(320*3, 920)];
    webView.delegate = self;
    [svMain setContentOffset:CGPointMake(0, 0)];
    svMain.frame = CGRectMake(0,36,320,svMain.frame.size.height);
    
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image_background.png"]];
    svMain.backgroundColor = background;

	NSURL *url=[NSURL URLWithString:url_regAgree];
    req = [ASIFormDataRequest requestWithURL:url];
    [req setDidFinishSelector:@selector(finishReq:)];
    isReqFinished = false;
    [req setDelegate:self];
    
    [req startAsynchronous];

}

- (void)viewDidUnload
{
    [self setSvMain:nil];
    [super viewDidUnload];
    [self setWebView:nil];
    // Release any retained subviews of the main view.
}
-(void) viewWillDisappear:(BOOL)animated{
    if (!isReqFinished){
        [req clearDelegatesAndCancel];
        [req cancel];
    }
}
- (void)finishReq:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    isReqFinished = true;
    NSString *jsonStr = [request responseString];
    NSDictionary *dic = [jsonStr JSONValue];
    [webView loadHTMLString:[dic objectForKey:@"data"]
                   baseURL:nil];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    isReqFinished = true;
    NSError *error = [request error];
    NSLog(@"%@",error);
    if (error.code==net_failure_code) {
        [OMGToast showWithText:net_failure_msg bottomOffset:100];
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)goBack:(id)sender {
     [self.navigationController popViewControllerAnimated:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)wv
{
    CGFloat webViewHeight = 0.0f;
    if (wv.subviews.count > 0)
    {
        UIView *scrollerView = [wv.subviews objectAtIndex:0];//为什么要取第一个？
        if (scrollerView.subviews.count > 0)
        {
            UIView *webDocView = scrollerView.subviews.lastObject;
            if ([webDocView isKindOfClass:[NSClassFromString(@"UIWebDocumentView") class]])
            {
                webViewHeight = webDocView.frame.size.height;//获取文档的高度
                wv.frame= CGRectMake(0,72,webDocView.frame.size.width,webDocView.frame.size.height); //更新UIWebView 的高度
                svMain.contentSize=CGSizeMake(svMain.frame.size.width, webViewHeight+72);//调整底层的contensize 的大小
            }
        }
    }
}

@end
